<?php
namespace Sys\Controller;
use Common\Controller\BaseController;
class TeacherController extends BaseController{
	public function __construct(){
		parent::__construct();
	}

	public function index(){
		$res=M('teachers')->select();
		$this->assign('teachers',$res);
		$this->display();
	}
	public function addTeacher(){//dump($_POST);
		 $role = M('roles')-> field('role_name,role_id')->select();
		$this->assign(role,$role);
		$this-> display();
	}
		public function doAddTeacher(){
			// 
			// exit;
		$this-> upload();
		D('teachers')-> create();
		 D('teachers')-> add();
		 $this->success('添加成功','index');
		
	}
	public function show(){
		$where = [
			'teacher_id' => I('get.id'),
		];
		 $res = M('teachers')-> where($where)-> find();
		$this-> assign('detail',$res);
		$this-> display();
	}
	public function doDelTeacher(){
		$ids = I('post.ids');
		$taf = M('teachers')-> delete($ids);
		if(!$taf){
			$msg = [
				'msg' 	=> '删除失败',
				'error' => 0,
			];

		}
	}
	public function doEditTeacher(){
        $res=D('teachers');
			if($res->create()){//dump($e);
            $res->save();
			$this->success('修改成功',U("index"));
			}else{
			$this->error('failed!');
		}
		

    }

	private function upload(){
	    $upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
	    $upload->savePath  =     '/portrait/'; // 设置附件上传（子）目录
    // 上传文件 
    	$info   =   $upload->upload();
	    if(!$info) {// 上传错误提示错误信息
	        $this->error($upload->getError());
	    }else{// 上传成功
	    	$_POST['lg_photo'] = './Uploads'.$info['photo']['savepath'].$info['photo']['savename'];
	    	$this-> thumb($_POST['lg_photo'],$info['photo']['ext']);
	    	// $this->success('上传成功！');
	    }
	}
	private function thumb($path,$ext){
		$_POST['md_photo'] = $path.'_middle'.'.'.$ext;
		$_POST['sm_photo'] = $path.'_small'.'.'.$ext;
		$image = new \Think\Image(); 
		$case = $image->open($path);
		if(!$case){
			$this->error('上传图片失败');
		}
		// 生成一个居中裁剪为150*150的缩略图并保存为thumb.jpg
		$image->thumb(150, 150,\Think\Image::IMAGE_THUMB_FIXED)->save($_POST['md_photo']);		
		$image->thumb(100, 100,\Think\Image::IMAGE_THUMB_FIXED)->save($_POST['sm_photo']);
	}
		
	
	
}
